/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WebServiceGamingServer;

import WebServiceDBController.GamingStateDB;
import WebServiceModel.GamingStateModel;
import java.util.ArrayList;

/**
 *
 * @author Peach
 */
public class GamingServer {
    public ArrayList<Integer> getMessage(int room,int desk){
	//return current desk state;
        GamingStateDB db=new GamingStateDB();
        GamingStateModel state=GamingStateModel.getInstance();
        state.setDeskID(room, desk);
        return changeToArray(db.GetState(state));
    }
	
    public void sendMessage(ArrayList<Integer> state){
        GamingStateDB db=new GamingStateDB();
        db.ModifyOption(changeToModel(state));
    }
    
    public void gameOver(int room,int desk){
        GamingStateDB db=new GamingStateDB();
        GamingStateModel state=GamingStateModel.getInstance();
        state.setDeskID(room, desk);
        db.ClearOption(state);
    }
    
    private ArrayList<Integer> changeToArray(GamingStateModel state){
        ArrayList<Integer> stateArray=new ArrayList<Integer>();
        stateArray.add(state.getRoomID());
        stateArray.add(state.getDeskID());
        stateArray.add(state.getState());
        stateArray.add(state.getPos().x);
        stateArray.add(state.getPos().y);
        stateArray.add(state.getTime());
        return stateArray;
    }
    
    private GamingStateModel changeToModel(ArrayList<Integer> state){
        GamingStateModel stateModel= new GamingStateModel();
        stateModel.setDeskID(state.get(0), state.get(1));
        stateModel.setState(state.get(2));
        stateModel.setPos(state.get(3), state.get(4));
        stateModel.setTime(state.get(5));        
        return stateModel;
    }
}
